package cn.wolfcode.web.controller;

import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.mq.OrderMQResult;
import cn.wolfcode.mq.OrderMessage;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * 消费者监听消息
 */
@Component
@RocketMQMessageListener(consumerGroup = "orderPeddingGroup",topic = MQConstant.ORDER_PEDDING_TOPIC)
public class OrderPeddingMessageListener implements RocketMQListener<OrderMessage> {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    // 消费消息：生成订单
    @Override
    public void onMessage(OrderMessage orderMessage) {
        // 执行完业务之后，往另一个MQ中发送消息，让webSocket监听消息进行通知
        OrderMQResult result = new OrderMQResult();
        result.setToken(orderMessage.getToken());
        // 标签tag
        String tag = ":";
        try {
            // 成功生成订单
            // orderNo订单号
            String orderNo = orderInfoService.doSecKill(orderMessage.getTime()
                    , orderMessage.getSeckillId()
                    , orderMessage.getPhone());
            result.setOrderNo(orderNo);
            // 超时取消问题的处理
            // 成功的标签
            //tag += MQConstant.ORDER_RESULT_SUCCESS_TAG;
            Message<String> msg = MessageBuilder.withPayload(orderNo).build();
            rocketMQTemplate.syncSend(MQConstant.ORDER_PAY_TIMEOUT_TOPIC,msg,3000,MQConstant.ORDER_PAY_TIMEOUT_DELAY_LEVEL);
        } catch (Exception ex) {
            // 生成订单失败
            result.setCode(SeckillCodeMsg.SECKILL_ERROR.getCode());
            result.setMsg(SeckillCodeMsg.SECKILL_ERROR.getMsg());
            // 少卖的处理
            // 添加场次和秒杀商品id以供成功进入消息中间件的生成订单的请求失败同步数据库库存到Redis中
            result.setTime(orderMessage.getTime());
            result.setSeckillId(orderMessage.getSeckillId());
            // 最终标签tag
            tag += MQConstant.ORDER_RESULT_FAIL_TAG;
            // 发送同步消息到消息中间件中被一个消费者消费，以供经过缓存筛选进入到第一个中间件的订单生成
            // 请求出现异常然后同步正常的库存到redis只能
            rocketMQTemplate.syncSend(MQConstant.ORDER_RESULT_TOPIC+tag,result);
        }
        // 发送消息，发送到另一个消费者中，推送消费结果
        rocketMQTemplate.asyncSend(MQConstant.ORDER_RESULT_TOPIC, result, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送消息到结果消费者成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("生成订单失败，请联系管理员");
            }
        });
    }
}
